// 导入数据库操作模块
const { Dingdan, Store, Finish } = require('../db/index')

// 管理者获取所有订单
exports.dingdan = (req, res) => {
    // 输出dingdan内容
    Dingdan.find().then(results => {
        // console.log(results);
        res.send({
            status: 0,
            message: '获取订单信息成功',
            data: results
        })
    })
}
// 用户获取自己的订单
exports.userDingdan = (req, res) => {
    // 接收表单数据
    const userinfo = req.body
    console.log(userinfo);
    Dingdan.find({ person: userinfo.person }).then(results => {
        res.send({
            status: 0,
            message: '获取成功',
            data: results
        })
    })
}
// 用户获取已完成的订单
exports.finish = (req, res) => {
    // 接收表单数据
    const userinfo = req.body
    console.log(userinfo);
    Finish.find({ person: userinfo.person }).then(results => {
        res.send({
            status: 0,
            message: '查询成功',
            data: results
        })
    })
}
// 查询已完成订单
exports.finishSearch = (req,res) => {
    // 接收表单数据
    const userinfo = req.body
    console.log(userinfo); 
    if (userinfo.name !== '') {
        Finish.find({ name: userinfo.name, person: userinfo.person }).then(results => {
            // if (results.length !== 1) return res.cc('查询失败')
            res.send({
                status: 0,
                message: '查询成功',
                data: results
            })
        })
    } else if (userinfo.name == '') {
        Finish.find({ person: userinfo.person }).then(results => {
            res.send({
                status: 0,
                message: '查询成功 ',
                data: results
            })
        })
    }
}


// 管理者查询订单
exports.check = (req, res) => {
    // 接收表单数据
    const userinfo = req.body
    console.log(userinfo);
    if (userinfo.name !== '') {
        Dingdan.find({ name: userinfo.name }).then(results => {
            console.log(results);
            // if (results.length !== 1) return res.cc('查询失败')
            res.send({
                status: 0,
                message: '查询成功',
                data: results
            })
        })
    } else if (userinfo.name == '') {
        Dingdan.find().then(results => {
            res.send({
                status: 0,
                message: '查询成功 ',
                data: results
            })
        })
    }

}
// 用户查询订单
exports.userCheck = (req, res) => {
    // 接收表单数据
    const userinfo = req.body
    console.log(userinfo);
    if (userinfo.name !== '') {
        Dingdan.find({ name: userinfo.name, person: userinfo.person }).then(results => {
            // if (results.length !== 1) return res.cc('查询失败')
            res.send({
                status: 0,
                message: '查询成功',
                data: results
            })
        })
    } else if (userinfo.name == '') {
        Dingdan.find({ person: userinfo.person }).then(results => {
            res.send({
                status: 0,
                message: '查询成功 ',
                data: results
            })
        })
    }

}


// 管理员添加订单
exports.add = (req, res) => {
    // 接收表单数据
    const userinfo = req.body
    console.log(userinfo);
    // 查询是否有库存
    Store.find({ name: userinfo.name }).then(results => {
        if (results.length !== 0) {          // 判断是否有该物品，若有则进行接下来的操作
            // 获得库存总量
            const storeNum = Number(results[0].number)
            // 获得订单数量
            const userinfoNum = Number(userinfo.num)
            // 得到库存剩余量
            const remainderNum = storeNum - userinfoNum
            if (storeNum >= userinfoNum) {
                Dingdan.create({
                    name: userinfo.name,
                    class: userinfo.class,
                    price: userinfo.price,
                    num: userinfo.num
                }).then()
                Store.updateOne({ name: userinfo.name }, { number: remainderNum }).then()
                Dingdan.find().then(results => {
                    res.send({
                        status: 0,
                        message: '添加成功',
                        data: results
                    })
                })
            } else if (storeNum < userinfoNum) {
                res.send({
                    status: 1,
                    message: '库存短缺，请联系管理员及时补充',
                    data: results
                })
            }
        } else if (results.length == 0) {
            res.send({
                status: 1,
                message: '库存没有此物品，请联系管理员',
            })
        }

    })
}
// 用户添加订单
exports.userAdd = (req, res) => {
    // 接收表单数据
    const userinfo = req.body
    console.log(userinfo);
    Dingdan.create({
        name: userinfo.name,
        class: userinfo.class,
        price: userinfo.price,
        num: userinfo.num,
        person: userinfo.person
    })
    Dingdan.find({ person: userinfo.person }).then(results => {
        res.send({
            status: 0,
            message: '添加成功',
            data: results
        })
    })
}


// 管理员删除订单
exports.delete = (req, res) => {
    // 接收表单数据
    const userinfo = req.body
    console.log(userinfo);

    Dingdan.findOneAndDelete({ _id: userinfo.id }).then(results => {
        Finish.create({
            name: userinfo.name,
            class: userinfo.class,
            price: userinfo.price,
            num: userinfo.num,
            person: userinfo.person
        }).then()
        res.send({
            status: 0,
            message: '删除成功',
        })
    })
}
// 用户删除订单
exports.userDelete = (req, res) => {
    // 接收表单数据
    const userinfo = req.body
    console.log(userinfo);
    Dingdan.findOneAndDelete({ _id: userinfo.id }).then()
}


// 管理员修改订单
exports.revise = (req, res) => {
    // 接收表单数据
    const userinfo = req.body
    // console.log(userinfo.num);
    Store.find({ name: userinfo.name }).then(results => {
        // console.log(results[0].number);
        const changeNum = Number(results[0].number) - userinfo.num
        console.log(changeNum);
        Store.updateOne({ name: userinfo.name }, { number: changeNum }).then()
    })
    Dingdan.updateOne({ id: userinfo.id }, { name: userinfo.name, class: userinfo.class, price: userinfo.price, num: userinfo.number }).then()
}
// 用户修改订单
exports.userRevise = (req, res) => {
    // 接收表单数据
    const userinfo = req.body
    console.log(userinfo);
    Dingdan.updateOne({ _id: userinfo.id }, { name: userinfo.name, class: userinfo.class, price: userinfo.price, num: userinfo.num }).then()
}